package src;

public class Processor implements Runnable
{
    private int cycleTime = 100;
    private boolean running = true;

    public Processor()
    {
    }

    public void run()
    {
        long curTime = System.currentTimeMillis();
        long timeSpent = 0;
        long lastGC = System.currentTimeMillis();
        while(running)
        {
            curTime = System.currentTimeMillis();
            Main.m.ge.process();
            if(curTime - lastGC >= 60000) //Every 60 seconds.
            {
                lastGC = System.currentTimeMillis();
                System.gc();
                Runtime runtime = Runtime.getRuntime();
                // Main.m.log("Memory usage " + (int)((runtime.totalMemory() - runtime.freeMemory()) / 1024L) + "K");
            }
            timeSpent = System.currentTimeMillis() - curTime;
            // Main.m.log("Time spent processing " + timeSpent);
            if(timeSpent >= cycleTime)
            {
                timeSpent = cycleTime;
            }
            try
            {
                Thread.sleep(cycleTime - timeSpent);
            }
            catch(InterruptedException ie)
            {
                ie.printStackTrace();
            }
        }
    }
}